package streambyte

import (
	"reflect"
	"streambyte/rtype"
	"unsafe"
)

// ValueMarshal 编码接口
type ValueMarshal interface {
	Marshal(eface *rtype.EmptyInterface, w *Write) bool
}

// ValueUnmarshal 解码接口
type ValueUnmarshal interface {
	Unmarshal(eface *rtype.EmptyInterface, r *Read) bool
}

// ValueEncode 编码类型
type ValueEncode interface {
	ValueMarshal
	ValueUnmarshal
}

// ValueList 支持编码列表
// map[类型]编解码实现
var ValueList map[string]ValueEncode

// 基础类型转换

type _bool struct{}

func (_bool) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Bool(*(*bool)(eface.Word))
	return true
}
func (_bool) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*bool)(eface.Word) = r.Bool()
	return true
}

type _int struct{}

func (_int) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Int(*(*int)(eface.Word))
	return true
}
func (_int) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*int)(eface.Word) = r.Int()
	return true
}

type _int8 struct{}

func (_int8) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Int8(*(*int8)(eface.Word))
	return true
}
func (_int8) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*int8)(eface.Word) = r.Int8()
	return true
}

type _int16 struct{}

func (_int16) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Int16(*(*int16)(eface.Word))
	return true
}
func (_int16) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*int16)(eface.Word) = r.Int16()
	return true
}

type _int32 struct{}

func (_int32) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Int32(*(*int32)(eface.Word))
	return true
}
func (_int32) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*int32)(eface.Word) = r.Int32()
	return true
}

type _int64 struct{}

func (_int64) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Int64(*(*int64)(eface.Word))
	return true
}
func (_int64) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*int64)(eface.Word) = r.Int64()
	return true
}

type _uint struct{}

func (_uint) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Uint(*(*uint)(eface.Word))
	return true
}
func (_uint) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*uint)(eface.Word) = r.Uint()
	return true
}

type _uint8 struct{}

func (_uint8) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Uint8(*(*uint8)(eface.Word))
	return true
}
func (_uint8) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*uint8)(eface.Word) = r.Uint8()
	return true
}

type _uint16 struct{}

func (_uint16) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Uint16(*(*uint16)(eface.Word))
	return true
}
func (_uint16) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*uint16)(eface.Word) = r.Uint16()
	return true
}

type _uint32 struct{}

func (_uint32) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Uint32(*(*uint32)(eface.Word))
	return true
}
func (_uint32) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*uint32)(eface.Word) = r.Uint32()
	return true
}

type _uint64 struct{}

func (_uint64) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Uint64(*(*uint64)(eface.Word))
	return true
}
func (_uint64) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*uint64)(eface.Word) = r.Uint64()
	return true
}

type _float32 struct{}

func (_float32) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Float32(*(*float32)(eface.Word))
	return true
}
func (_float32) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*float32)(eface.Word) = r.Float32()
	return true
}

type _float64 struct{}

func (_float64) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Float64(*(*float64)(eface.Word))
	return true
}
func (_float64) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*float64)(eface.Word) = r.Float64()
	return true
}

type _complex64 struct{}

func (_complex64) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Complex64(*(*complex64)(eface.Word))
	return true
}
func (_complex64) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*complex64)(eface.Word) = r.Complex64()
	return true
}

type _complex128 struct{}

func (_complex128) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Complex128(*(*complex128)(eface.Word))
	return true
}
func (_complex128) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*complex128)(eface.Word) = r.Complex128()
	return true
}

/*
 * 处理复合类型的数据结构
 *  @! 底层修改后会导致无法使用标记
 *  @? 表示代码需关注
 *  @? 关键注释
 */

type _uintptr struct{}

func (_uintptr) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	// 还原指针
	typ := &rtype.EmptyInterface{}
	typ.Typ = (*rtype.PtrType)(unsafe.Pointer(eface.Typ)).Elem
	kind := typ.Typ.Kind()
	// 类型还原
	switch kind {
	case reflect.Map:
		typ.Word = *(*unsafe.Pointer)(eface.Word)
	default:
		typ.Word = eface.Word
	}
	// 指向的类型必须是指针
	if kind == reflect.Ptr {
		// 指向的指针必须可以设置值
		if typ.Typ.Flag()&(rtype.FlagAddr|rtype.FlagRO) != rtype.FlagAddr {
			typ.Word = *(*unsafe.Pointer)(typ.Word)
		}
	}
	// 编码类型
	if f, ok := ValueList[typ.Typ.TypeName()]; ok {
		return f.Marshal(typ, w)
	}
	return false
}
func (_uintptr) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	// 还原指针
	typ := &rtype.EmptyInterface{}
	typ.Typ = (*rtype.PtrType)(unsafe.Pointer(eface.Typ)).Elem
	// 类型指针还原
	switch typ.Typ.Kind() {
	case reflect.Map:
		typ.Word = *(*unsafe.Pointer)(eface.Word)
	default:
		typ.Word = eface.Word
	}
	// 编码类型
	if f, ok := ValueList[typ.Typ.TypeName()]; ok {
		return f.Unmarshal(typ, r)
	}
	return false
}

type _map struct{}

func (_map) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	//@? 结构体参数开始基址地址
	ptr := eface.Pointer()
	if ptr == nil {
		ptr = eface.Word
	}
	//@? 得到 map 内容大小
	len := rtype.Maplen(ptr)
	w.Int(len)
	if len == 0 {
		return false
	}
	//@? 得到 map 内容类型
	mapType := (*rtype.MapType)(unsafe.Pointer(eface.Typ))
	if mapType == nil || mapType.Key == nil || mapType.Elem == nil {
		return false
	}
	//@? 通过 map 类型定义,分别得到编码对象
	var (
		//@? 对应编码实现
		keyfunc  ValueMarshal
		elemfunc ValueMarshal
	)
	if v, ok := ValueList[mapType.Key.TypeName()]; ok {
		keyfunc = v
	} else {
		return false
	}
	if v, ok := ValueList[mapType.Elem.TypeName()]; ok {
		elemfunc = v
	} else {
		return false
	}
	//@? 构建 map 底层结构
	var (
		key, elme unsafe.Pointer
		rtypes    rtype.EmptyInterface
	)
	it := rtype.Mapiterinit(eface.Typ, ptr)
	for {
		//@? 得到数据
		key = rtype.Mapiterkey(it)
		if key == nil {
			break
		}
		elme = rtype.Mapiterelem(it)
		//@? 输出数据
		rtypes.Typ = mapType.Key
		rtypes.Word = key
		keyfunc.Marshal(&rtypes, w)
		rtypes.Typ = mapType.Elem
		rtypes.Word = elme
		elemfunc.Marshal(&rtypes, w)
		//@? 得到下一个数据
		rtype.Mapiternext(it)
	}
	return true
}
func (_map) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	tt := (*rtype.MapType)(unsafe.Pointer(eface.Typ))
	//@? 反序列化参数
	var (
		//@? 对应反序列化函数实现
		keyfunc  ValueUnmarshal
		elemfunc ValueUnmarshal
	)
	//@? 得到反序列化函数实现
	{
		if v, ok := ValueList[tt.Key.TypeName()]; ok {
			keyfunc = v
		} else {
			return false
		}
		if v, ok := ValueList[tt.Elem.TypeName()]; ok {
			elemfunc = v
		} else {
			return false
		}
	}
	//@? 构建序列化数据结构
	var key rtype.EmptyInterface
	key.Typ = tt.Key
	var elem rtype.EmptyInterface
	elem.Typ = tt.Key
	//@? 递归读取
	count := r.Int()
	for i := 0; i < count; i++ {
		k := rtype.Unsafe_New(tt.Key)
		e := rtype.Unsafe_New(tt.Elem)
		key.Word = k
		if !keyfunc.Unmarshal(&key, r) {
			return false
		}
		elem.Word = e
		if !elemfunc.Unmarshal(&elem, r) {
			return false
		}
		rtype.Mapassign(eface.Typ, eface.Pointer(), k, e)
	}
	return true
}

type _string struct{}

func (_string) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	w.Bytes([]byte(*(*string)(eface.Word)))
	return true
}

func (_string) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	*(*string)(eface.Word) = string(r.Bytes())
	return true
}

//@_struct 结构提转化要求
//+ Unmarshal 反序列化时候结构体需要初始化,并且指针传值给反序列化函数
type _struct struct{}

func (_struct) Marshal(eface *rtype.EmptyInterface, w *Write) bool {
	st := (*rtype.StructType)(unsafe.Pointer(eface.Typ))
	in := &rtype.EmptyInterface{}
	for i, count := 0, len(st.Fields); i < count; i++ {
		typ := st.Fields[i]
		kind := typ.Typ.Kind()
		in.Word = unsafe.Pointer(uintptr(eface.Word) + typ.Offset())
		if kind == reflect.Ptr {
			in.Typ = (*rtype.PtrType)(unsafe.Pointer(typ.Typ)).Elem
			in.Word = *(*unsafe.Pointer)(in.Word)
			kind = in.Typ.Kind()
			switch kind {
			case reflect.Map:
				in.Word = *(*unsafe.Pointer)(in.Word)
			}
		}
		if v, ok := ValueList[kind.String()]; ok {
			if !v.Marshal(in, w) {
				return false
			}
		}
	}
	return true
}
func (_struct) Unmarshal(eface *rtype.EmptyInterface, r *Read) bool {
	st := (*rtype.StructType)(unsafe.Pointer(eface.Typ))
	in := &rtype.EmptyInterface{}
	for i, count := 0, len(st.Fields); i < count; i++ {
		typ := st.Fields[i]
		kind := typ.Typ.Kind()
		in.Word = unsafe.Pointer(uintptr(eface.Word) + typ.Offset())
		if kind == reflect.Ptr {
			in.Typ = (*rtype.PtrType)(unsafe.Pointer(typ.Typ)).Elem
			in.Word = *(*unsafe.Pointer)(in.Word)
			kind = in.Typ.Kind()
			switch kind {
			case reflect.Map:
				in.Word = *(*unsafe.Pointer)(in.Word)
			}
		}
		if v, ok := ValueList[kind.String()]; ok {
			if !v.Unmarshal(in, r) {
				return false
			}
		}
	}
	return true
}

// init 初始化
func init() {
	ValueList = make(map[string]ValueEncode)
	//+ 将默认对象作初始化记录
	ValueList["bool"] = new(_bool)
	ValueList["int"] = new(_int)
	ValueList["int8"] = new(_int8)
	ValueList["int16"] = new(_int16)
	ValueList["int32"] = new(_int32)
	ValueList["int64"] = new(_int64)
	ValueList["uint"] = new(_uint)
	ValueList["uint8"] = new(_uint8)
	ValueList["uint16"] = new(_uint16)
	ValueList["uint32"] = new(_uint32)
	ValueList["uint64"] = new(_uint64)
	ValueList["float32"] = new(_float32)
	ValueList["float64"] = new(_float64)
	ValueList["complex64"] = new(_complex64)
	ValueList["complex128"] = new(_complex128)
	//+ 复合结构
	ValueList["ptr"] = new(_uintptr)
	ValueList["map"] = new(_map)
	ValueList["string"] = new(_string)
	ValueList["struct"] = new(_struct)
}
